
const ArticleModel = require('../model/Article')
const Sequelize = require('sequelize');
exports.create = async ctx => {
    let { user_id,type_id,title, type, content } = ctx.request.body
    await ArticleModel.create({user_id,type_id,title,type,content })
    ctx.send('', '发布成功')
}

exports.delete = async ctx => {
    let id = ctx.query.id
    // 软删
    await ArticleModel.update({del: 1}, {where: {id: id}})
    ctx.send('', '删除成功')
}

exports.update = async ctx => {
    let { type_id,title, type, content, id } = ctx.request.body
    await ArticleModel.update({type_id,title,type,content}, {where: {id: id}})
    ctx.send('', '更新成功')
}

exports.findAll = async ctx => {
    let { page, limit } = ctx.query
    let list = await ArticleModel.findAll({limit, offset: page * limit}, {attributes: { exclude: ['created_at', 'updated_at']}})

    ctx.send({list}, '')
}

exports.findOne = async ctx => {
    let { id } = ctx.query
    let data = await ArticleModel.findOne({attributes: ['id', ['user_id', 'userId'], 'type_id', 'title', 'content', 'view', 'del']}, {where: {id}})
    ctx.send(data, '')
}